/*******************************************************************************
 * Copyright 2010 MoBrite (A venture of Kinara Systems Private Limited)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.mobrite.domaindroid.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

public class Util {

//	public static void debug (String msg, Throwable t) {
//		if (debug) 
//			Log.d("Domain Droid", msg, t);
//	}
//	
//	public static void debug (String msg) {
//		if (debug) 
//			Log.d("Domain Droid", msg);
//	}
//
//	public static void debug(String tag, String msg) {
//		if (debug) 
//			Log.d(tag, msg);
//	}

	public static String getFormattedDateForDb (long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = sdf.format(new Date(time - TimeZone.getDefault().getOffset(time)));
        return formattedDate;
	}
	
	public static String getFormattedDateForDisplay (Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("d MMM, yyyy 'at' h:mm a");
        String formattedDate = sdf.format(date);        
        return formattedDate;		
	}
	
	public static long getTimeFromDbString (String dbDateString) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
			return sdf.parse(dbDateString).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
			return 0; // TODO Need better way to flag error. Throw an exception here
		}
	}
	
	public static String getDateForListDisplay (Context context, long when) {
		
		int flags = 0;
		flags |= android.text.format.DateUtils.FORMAT_SHOW_DATE;
		flags |= android.text.format.DateUtils.FORMAT_ABBREV_MONTH;
		//flags |= android.text.format.DateUtils.FORMAT_SHOW_YEAR;
		flags |= android.text.format.DateUtils.FORMAT_SHOW_TIME;
		
		String finalDateTime = android.text.format.DateUtils.formatDateTime(context,
				when + TimeZone.getDefault().getOffset(when), flags);
		
		//debug("Formatted Date: "+finalDateTime);
		
		return finalDateTime;
		
	}
	
	public static void showMessageBox (Activity context, String title, String message) {
		
		new AlertDialog.Builder(context)
		.setTitle(title)
		.setMessage(message)
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {						
			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		})
		.show();

	}

	public static String getDomainWithoutTld(String domain) {		
		if (domain.indexOf(".")<=0) {
			return domain;
		}
		return domain.substring(0, domain.indexOf("."));
	}


}
